package com.example.demo.event;


import com.example.demo.entity.LogError;
import com.example.demo.service.ILogErrorService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 异步监听错误日志事件
 *
 * @author cici
 */
@Slf4j
@AllArgsConstructor
public class ErrorLogListener{

	private ILogErrorService logErrorService;

	@Async
	@EventListener(ErrorLogEvent.class)
	public void saveErrorLog(ErrorLogEvent event) {
		Map<String, Object> source = (Map<String, Object>) event.getSource();
		LogError logError = (LogError) source.get("log");
		log.info("进入ErrorLogListener");
		logErrorService.save(logError);
	}

}
